package com.intellagric.agriecom.service.agriecom_produce_param_value;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.agriecom.module.agriecom_produce_category.AgriecomProduceCategoryService;
import com.intellagric.agriecom.module.agriecom_produce_param.ParamService;
import com.intellagric.agriecom.module.agriecom_produce_param_value.ParamValueService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.agriecom.agriecom_produce_param_value.AgriecomProduceParamValueDao;
import com.intellagric.mapper.AgriecomProduceParamValueMapper;
import com.intellagric.pojo.AgriecomProduceParam;
import com.intellagric.pojo.AgriecomProduceParamExample;
import com.intellagric.pojo.AgriecomProduceParamValue;
import com.intellagric.pojo.AgriecomProduceParamValueExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ParamValueServiceImpl implements ParamValueService{
    @Autowired
    private AgriecomProduceParamValueMapper paramValueMapper;

    @Autowired
    private AgriecomProduceParamValueDao dao;

    @Autowired
    private AgriecomProduceCategoryService categoryService;

    @Autowired
    private ParamService paramService;

    /**
     * 添加
     * @param vo
     * @return
     */
    @Override
    public ResponseMessage addParamValue(AgriecomProduceParamValue vo) {
        int i=0;
        if(vo.getId()!=null&&vo.getId()!="")
            i=paramValueMapper.insert(vo);
        if (i!=0)
            return ResponseMessage.success();
        return ResponseMessage.fail();
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public AgriecomProduceParamValue selectParamValueById(String id) { return paramValueMapper.selectByPrimaryKey(id); }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public ResponseMessage deleteParamValueById(String id) {
        int i=paramValueMapper.deleteByPrimaryKey(id);
        if(i==1)
            return ResponseMessage.success();
        return ResponseMessage.fail();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public LayuiDataGridResult batchDelete(String[] ids) {
        int i=dao.batchDelete(ids);
        if(i!=0)
            return LayuiDataGridResult.success();
        return LayuiDataGridResult.fail();
    }

    /**
     * 匹配id修改
     * @param vo
     * @return
     */
    @Override
    public ResponseMessage updateParamValueByVo(AgriecomProduceParamValue vo) {
        AgriecomProduceParamValueExample example=new AgriecomProduceParamValueExample();
        example.createCriteria().andIdEqualTo(vo.getId());
        int i=paramValueMapper.updateByExampleSelective(vo,example);
        if(i==1)
            return ResponseMessage.success();
        return ResponseMessage.fail();
    }

    /**
     * 分页获取所有内容
     * @param page
     * @param limit
     * @return
     */
    @Override
    public LayuiDataGridResult getParamValueList(int page, int limit) {
        PageHelper.startPage(page,limit);
        AgriecomProduceParamValueExample example=new AgriecomProduceParamValueExample();
        example.createCriteria().andParamIdIsNotNull();
        PageInfo info=new PageInfo(paramValueMapper.selectByExample(example));
        return LayuiDataGridResult.success().add(info.getList(),info.getTotal());
    }

    /**
     * 根据分类id查找列表
     * @param categoryId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public LayuiDataGridResult getParamListByParam(String categoryId, int page, int limit) {
        List<AgriecomProduceParamValue> result=new ArrayList<>();
        //获取此产品分类下的所有参数
        List<AgriecomProduceParam> list=paramService.getParamListByCategory(categoryId).getData();
        for (AgriecomProduceParam param:list){
            //获取参数值
            AgriecomProduceParamValueExample example=new AgriecomProduceParamValueExample();
            example.createCriteria().andParamIdEqualTo(param.getId());
            List<AgriecomProduceParamValue> values=paramValueMapper.selectByExample(example);
            result.addAll(values);
        }
        //分页？？？？

        if(((page-1)*limit)+limit<=result.size())
            return LayuiDataGridResult.success().add(result.subList((page-1)*limit,((page-1)*limit)+limit),result.size());
        return LayuiDataGridResult.success().add(result.subList((page-1)*limit,result.size()),result.size());
    }

    @Override
    public LayuiDataGridResult getListByKeyword(int page, int limit, String keyword) {
        PageHelper.startPage(page,limit);
        AgriecomProduceParamValueExample paramValueExample=new AgriecomProduceParamValueExample();
        paramValueExample.createCriteria().andParamValueLike("%"+keyword+"%");
        List<AgriecomProduceParamValue> list=paramValueMapper.selectByExample(paramValueExample);
        PageInfo info=new PageInfo(list);
        return LayuiDataGridResult.success().add(info.getList(),info.getTotal());
    }



}
